import 'package:flutter/material.dart';
import 'package:flutterproject/mvvm/base/base_state.dart';
import 'package:flutterproject/page/login_all/login/login_page.dart';
import 'package:flutterproject/page/setting/children/address/address_page.dart';
import 'package:flutterproject/page/setting/children/child_account/child_account_page.dart';
import 'package:flutterproject/page/setting/children/modify_login_pwd/modify_login_pwd_page.dart';
import 'package:flutterproject/page/setting/children/modify_pay_pwd/modify_pay_pwd_page.dart';
import 'package:flutterproject/page/setting/children/upgrade/upgrade_page.dart';
import 'package:flutterproject/page/setting/controller/setting_controller.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/util/sp_util/shared_preferences.dart';
import 'package:flutterproject/util/sp_util/sp_key.dart';
import 'package:flutterproject/widget/app_bar_common.dart';
import 'package:flutterproject/widget/button_common.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';

class SettingPage extends BaseState<SettingController> {
  @override
  SettingController initController() => SettingController();

  @override
  Widget initView(BuildContext context) {
    return GetBuilder<SettingController>(builder: (logic) {
      return ColoredBox(
        color: Color(0xFFF6F6F6),
        child: Column(
          children: [
            AppBarCommon(
              title: "账户设置",
              bgColor: Color(0xFFEDC88B),
              leftColor: Colors.white,
              titleColor: Colors.white,
            ),
            Container(
              padding: MarginPaddingUtil.only(top: 52, bottom: 31),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(20.sp),
                  bottomRight: Radius.circular(20.sp),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      47.widthBox,
                      Stack(
                        alignment: Alignment.center,
                        children: [
                          ImageCommon("/image/vip_level", size: 90.sp),
                          TextCommon(
                            "1",
                            size: 40.sp,
                            fontWeight: FontWeight.bold,
                            color: Colors.white,
                          )
                        ],
                      ),
                      24.widthBox,
                      TextCommon(
                        "${SPUtils().getLocalStorage(SPKey.mobile)}",
                        size: 48.sp,
                        fontWeight: FontWeight.bold,
                      )
                    ],
                  ),
                  Divider(
                    color: Color(0xFFE7E7E7),
                  ),
                  27.heightBox,
                  Padding(
                    padding: MarginPaddingUtil.symmetric(horizontal: 47),
                    child: Row(
                      children: [
                        TextCommon(
                          "收货地址管理",
                          size: 28.sp,
                        ),
                        Spacer(),
                        ImageCommon(
                          "/icon/icon_arrow_right",
                          width: 12.width,
                          height: 20.width,
                        )
                      ],
                    ),
                  ).addClickEvent(() => Get.to(() => AddressPage()))
                ],
              ),
            ),
            20.heightBox,
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20.sp),
              ),
              child: Column(
                children: [
                  _buildItem(
                      title: '修改登录密码',
                      onTap: () => Get.to(ModifyLoginPwdPage())),
                  Divider(
                    color: Color(0xFFE7E7E7),
                    height: 1.width,
                  ),
                  _buildItem(
                      title: '修改支付密码', onTap: () => Get.to(ModifyPayPwdPage())),
                  Divider(
                    height: 1.width,
                    color: Color(0xFFE7E7E7),
                  ),
                  _buildItem(
                      title: '子账号', onTap: () => Get.to(ChildAccountPage())),
                ],
              ),
            ),
            20.heightBox,
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20.sp),
              ),
              child: Column(
                children: [
                  _buildItem(
                      title: '系统版本',
                      onTap: () {
                        Get.to(UpgradePage());
                      },
                      right: controller.version),
                  Divider(
                    color: Color(0xFFE7E7E7),
                    height: 1.width,
                  ),
                  // _buildItem(title: '相关协议', onTap: () {}),
                ],
              ),
            ),
            Spacer(),
            Container(
              width: Get.width,
              color: Colors.white,
              padding:
                  MarginPaddingUtil.symmetric(vertical: 22, horizontal: 200),
              child: ButtonCommon(
                text: "退出登录",
                onTap: () async {
                  await SPUtils().remove(SPKey.token);
                  Get.offAll(LoginPage());
                },
                fontSize: 32.sp,
                fontColor: Colors.white,
                color: Color(0xFFEDC88B),
                borderRadios: 50,
                padding: MarginPaddingUtil.only(top: 25, bottom: 29),
              ),
            )
          ],
        ),
      );
    });
  }

  Widget _buildItem(
      {required String title, required VoidCallback onTap, String? right}) {
    return Padding(
      padding: MarginPaddingUtil.symmetric(horizontal: 47, vertical: 31),
      child: Row(
        children: [
          TextCommon(
            title,
            size: 28.sp,
            lineHeight: 40.sp,
          ),
          Spacer(),
          if (right != null)
            Row(
              children: [
                TextCommon(
                  right,
                  size: 24.sp,
                  color: Color(0xFF828282),
                  lineHeight: 40.sp,
                ),
                50.widthBox,
              ],
            ),
          ImageCommon(
            "/icon/icon_arrow_right",
            width: 12.width,
            height: 20.width,
          )
        ],
      ),
    ).addClickEvent(onTap);
  }
}
